import { User } from '@entity/user';
import { Axios } from '@axios/base'
import { HttpStatus } from '@axios/base/http_status';

export class UserBase extends Axios {

    constructor() {
        super('user');
    }

    /**
     * GET /user/query
     * @param id 用户 id 
     */
    async queryById(id: Number): Promise<any> {
        const result = await this.get<any>('/query', {
            params: {
                targetId: id
            }
        });
        return result.status == HttpStatus.SUCCESS ? result.data : undefined;
    }
    
    /**
     * GET /user/token
     * @param id 用户 id 
     */
     getToken(email: String): void {
        this.get<any>('/token', {
            params: {
                email: email
            }
        });
    }

    /**
     * POST /user/query
     * @param id 用户 id 
     */
     async queryByInfo(info: any = {}): Promise<Array<User>> {
        const result = await this.post<Array<User>>('/query', info);
        return result.status == HttpStatus.SUCCESS ? result.data : undefined;
    }

    /**
     * POST /user/register
     * @param user 注册信息
     */
    async register(user: any): Promise<Boolean> {
        const result = await this.post<Boolean>('/register', user)
        return result.status == HttpStatus.SUCCESS ? result.data : false
    }

    /**
     * POST /user/login
     * @param info 登录信息
     */
    async login(info: any): Promise<User> {
        const result = await this.post<User>('/login', info)
        return result.status == HttpStatus.SUCCESS ? result.data : new User()
    }

}
